package cn.ssm.petadopt.controller;

import cn.ssm.petadopt.dto.AdminsLoginDTO;
import cn.ssm.petadopt.dto.PwdSettingDTO;
import cn.ssm.petadopt.dto.SelectPageDTO;
import cn.ssm.petadopt.dto.UserLoginDTO;
import cn.ssm.petadopt.entity.Admins;
import cn.ssm.petadopt.service.AdminsService;
import cn.ssm.petadopt.util.ResponseResult;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.UUID;

/**
 * @Autheor: Sun俊仪
 * @Date: 2022 2022/12/17 14:59
 * @Name: PetSystem
 */
@Controller
@RequestMapping("admins")
public class AdminsController {

    @Autowired
    private AdminsService adminsService;

    // 用户登录
    @ResponseBody
    @RequestMapping("loginAdmins")
    public ResponseResult loginU(@RequestBody AdminsLoginDTO adminsLoginDTO , HttpSession session){
        System.out.println("adminsLoginDTO = " + adminsLoginDTO);
        ResponseResult responseResult = adminsService.loginA(adminsLoginDTO,session);
        return responseResult;
    }


    // 分页查询+搜索
    @ResponseBody
    @RequestMapping("selectTable")
    public ResponseResult loginU(SelectPageDTO selectPageDTO){
        System.out.println("selectPageDTO = " + selectPageDTO);
        ResponseResult responseResult = adminsService.selectPage(selectPageDTO);
        return responseResult;
    }

    // 修改个人信息
    @ResponseBody
    @RequestMapping("updateAdmin")
    public ResponseResult updateA(@RequestBody Admins admin,HttpSession session){
        System.out.println("admin = " + admin);
        ResponseResult responseResult = adminsService.updateA(admin,session);
        return responseResult;
    }

    // 修改个人密码
    @ResponseBody
    @RequestMapping("adminPwdSet")
    public ResponseResult adminPwdSet(@RequestBody PwdSettingDTO pwdSettingDTO, HttpSession session){
        System.out.println("pwdSettingDTO = " + pwdSettingDTO);
        ResponseResult responseResult = adminsService.updatePwd(pwdSettingDTO,session);
        return responseResult;
    }


    // 上传头像
    @RequestMapping("upload")
    public void uploadHeaderPicture(@RequestParam("file")MultipartFile multipartFile, HttpSession session, HttpServletResponse resp) throws IOException {
        ResponseResult responseResult = ResponseResult.builder().build();
        if (multipartFile.isEmpty()){
            responseResult.setCode(500);
            responseResult.setMsg("上传图片不能为空");
        }

        String originalFilename = multipartFile.getOriginalFilename();  // 原始名
        String filenameExtension = StringUtils.getFilenameExtension(originalFilename);  // 后缀
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String finalName = uuid+"."+filenameExtension;

        //上传到文件夹中
        File file = new File("F:/upload", finalName);
        try {
            multipartFile.transferTo(file);
            // 设置session域对象中的pic    同时存入数据库中
            Admins admin = (Admins) session.getAttribute("admin");
            admin.setPic(finalName);
            System.out.println(admin.getAdminName());
            // 存入数据库中
            Integer line = adminsService.uploadPic(admin);
            if (line!=null){
                responseResult.setCode(200);
                responseResult.setMsg("文件上传成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
            responseResult.setCode(500);
            responseResult.setMsg("文件上传失败");
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.write("头像上传成功");
        writer.close();
    }

    // 显示头像
    @RequestMapping("getPicture")
    public void getPic(HttpSession session,HttpServletResponse response){
        Admins admin = (Admins) session.getAttribute("admin");
        String pic = admin.getPic();
        File file = new File("F:/upload",pic);
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            ServletOutputStream outputStream = response.getOutputStream();
            IOUtils.copy(fileInputStream,outputStream);
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 判断管理员名称是否重复
    @RequestMapping("addJudgeAdminName")
    @ResponseBody
    public ResponseResult adminPwdSet(String adminName){
        System.out.println("adminName = " + adminName);
        ResponseResult responseResult = adminsService.addJudgeAdminName(adminName);
        return responseResult;
    }

    // 添加管理员admin
    @RequestMapping("addAdmin")
    @ResponseBody
    public ResponseResult addOneAdmin(@RequestBody Admins admin){
        System.out.println("admins = " + admin);
        ResponseResult responseResult = adminsService.addOneAdmin(admin);
        return responseResult;
    }

    // 编辑用户
    @ResponseBody
    @RequestMapping("showEditAdmin")
    public ResponseResult editShow(Integer id){
        System.out.println("id = " + id);
        ResponseResult responseResult = adminsService.selectAdminById(id);
        return responseResult;
    }
}
